﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTSystem : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 IsAdmin=1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                AjaxPro.Utility.RegisterTypeForAjax(typeof(EHTWebsiteProject.Administrator.EHTArticle));
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {
                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadLanguage(Thread.CurrentThread.CurrentCulture);
                    LoadTinTuc();
                    LoadSanPham();
                    LoadMenuSystem();
                }
            }
        }

         /// <summary>
    /// Ham load ngon ngu
    /// </summary>
    /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {            
            lbTinHot.Text = rm.GetString("lbTinHot", ci);
            lbSanPhamBanChay.Text = rm.GetString("lbSanPhamBanChay", ci);
            lbSystem.Text = rm.GetString("lbSystem",ci);
                        
        }

        [AjaxPro.AjaxMethod]
        public void LoadMenuSystem()
        { 
            MembershipUser theUser = Membership.GetUser(Session["UserName"].ToString());
            String roleName = Roles.GetRolesForUser(theUser.UserName)[0];
            if (roleName == HangSo.ROLE_ADMIN)
               IsAdmin = 1;
            else
               IsAdmin = 0;
            rpMenuSystem.DataSource = MenusProvider.GetMenus(theUser.ProviderUserKey.ToString(), IsAdmin,4);
            rpMenuSystem.DataBind();
        }

        [AjaxPro.AjaxMethod]
        public void LoadTinTuc()
        {
            rpNewsViews.DataSource = DanhMucSanPhamProvider.GetTinTuc_SanPhamHost().Tables[0];
            rpNewsViews.DataBind();
        }

        [AjaxPro.AjaxMethod]
        public void LoadSanPham()
        {
            rpProductsHost.DataSource = DanhMucSanPhamProvider.GetTinTuc_SanPhamHost().Tables[1];
            rpProductsHost.DataBind();
        }

        /// <summary>
        /// Ham cat chuoi URL
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
       
        public String SubStringUrl(String Input)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                Input.Replace("//", "?");
                String str1 = Input.Split('?')[1];
                sb.Append(str1.Split('?')[1]);
            }
            catch
            { 
                
            }
            return sb.ToString();
        }

        protected void rpMenuSystem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imgButton");
            Label lbIcon = (Label)e.Item.FindControl("lbIcon");
            imgButton.ImageUrl = "~/ImageDatas/Icons/" + lbIcon.Text;
        }
    }
}